﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows;
using MARS.analyticalengine;
using System.Drawing;
using System.Windows.Forms;




namespace Mars2013plugin
{
    public partial class MarsAnalyticsRegion
    {
        Outlook.MailItem localMailItem = null;
        public SentimentResult ser = null;
        public ProfanityResult pr = null;
        public StatisticalResult sr = null;

        
            
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
        [Microsoft.Office.Tools.Outlook.FormRegionName("Mars2013plugin.MarsAnalytics")]
        public partial class MarsFormRegionFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void MarsFormRegionFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
               
            
            }

        }

        #endregion
    
        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void MarsFormRegion_FormRegionShowing(object sender, System.EventArgs e)
        {
            if (Globals.ThisAddIn.MARSEngine == null)
            {
                //something Bad happened as the MARS Engine is still null. Most probably initialization failed.
                return;
            }

            if (!Globals.ThisAddIn.MarsRegionsDict.ContainsKey((Outlook.MailItem)this.OutlookItem))
            {
                Globals.ThisAddIn.MarsRegionsDict.Add((Outlook.MailItem)this.OutlookItem, this);
            }
            RunAnalysis();
        }

        public void RunAnalysis()
        {
           
            if (this.OutlookItem is Outlook.MailItem)
            {
                //if (this.OutlookFormRegion.FormRegionMode ==
                //    Outlook.OlFormRegionMode.olFormRegionRead)

                Outlook.MailItem mailItem =
                (Outlook.MailItem)this.OutlookItem;
                localMailItem = mailItem;

                if (Globals.ThisAddIn.MARSEngine == null)
                {
                    //something Bad happened as the MARS Engine is still null. Most probably initialization failed.
                    return;
                }
                 pr = Globals.ThisAddIn.MARSEngine.ProfanityAnalysis(mailItem.Body);
                switch (pr.EmailTone)
                {


                    case 1:
                        lbltone.Text = "mild";
                        tonepictureBox.BackColor = Color.Yellow;
                        break;

                    case 2:
                        lbltone.Text = "rude";
                        tonepictureBox.BackColor = Color.OrangeRed;
                        break;

                    default:
                        lbltone.Text = "none";
                        tonepictureBox.BackColor = Color.Lime;

                        break;

                }
                lblprofanecount.Text = pr.ProfaneWordCount.ToString();
                lblmildcount.Text = pr.MildWordCount.ToString();
                string logtext = "Rude words found:" + pr.ProfaneWordCount.ToString() + "\n";
                foreach (string s in pr.Matchprofanewords)
                {
                    logtext += s + "\n";
                }

                logtext += "Mild words found:" + pr.MildWordCount.ToString() + "\n";
                foreach (string s in pr.Matchmildwords)
                {
                    logtext += s + "\n";
                }
                richTextBox1.Text = logtext;
                //SentimentResult ser = ma.SentimentAnalysis(mailItem.Body);
                //statistical analysis
                sr = Globals.ThisAddIn.MARSEngine.StatisticalAnalysis(mailItem.Body);

                //   richTextBox1.Text = "Subject :" + mailItem.Subject + "\n" + "Body:\n" + mailItem.Body;

                //long l = WordCount(mailItem.Body);
                this.wordcnt.Text = sr.Words.ToString();
                //l = CountLinesInString(mailItem.Body);
                this.linecnt.Text = sr.Lines.ToString();
                this.charcnt.Text = sr.Characters.ToString();


                this.Recipientcnt.Text = mailItem.Recipients.Count.ToString();
                this.Attachmentcnt.Text = mailItem.Attachments.Count.ToString();

                setCurrentSentimentPicturebox();

            }
            else
            {
                richTextBox1.Text = "CURRENT ITEM IS NOT A MAIL ITEM";
            }
        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void MarsFormRegion_FormRegionClosed(object sender, System.EventArgs e)
        {
        }

        public string richTextBox {
            get { return this.richTextBox1.Text;} 
            set { richTextBox1.Text = value; }
        }
        
     

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void SentimentValue_Click(object sender, EventArgs e)
        {

        }

        private void pictureBoxverypositive_MouseEnter(object sender, EventArgs e)
        {
            pictureBoxverypositive.Image = Properties.Resources.circle_green;
        }

        private void pictureBoxverypositive_MouseLeave(object sender, EventArgs e)
        {
            setCurrentSentimentPicturebox();
            //pictureBoxverypositive.Image = Properties.Resources.circle_green_light;
        }

        private void pictureBoxpositive_MouseEnter(object sender, EventArgs e)
        {
            //pictureBoxpositive.Image = Properties.Resources.circle_blue;
        }
        private void pictureBoxpositive_MouseLeave(object sender, EventArgs e)
        {
            setCurrentSentimentPicturebox();
            //pictureBoxpositive.Image = Properties.Resources.circle_blue_light;
        }

        private void pictureBoxneutral_MouseEnter(object sender, EventArgs e)
        {
            pictureBoxneutral.Image = Properties.Resources.round_grey;
        }

        private void pictureBoxneutral_MouseLeave(object sender, EventArgs e)
        {
            setCurrentSentimentPicturebox();
           // pictureBoxneutral.Image = Properties.Resources.round_grey_light;
        }

        private void pictureBoxnegative_MouseEnter(object sender, EventArgs e)
        {
            //pictureBoxnegative.Image = Properties.Resources.circle_orange;
        }

        private void pictureBoxnegative_MouseLeave(object sender, EventArgs e)
        {
            setCurrentSentimentPicturebox();
            //pictureBoxnegative.Image = Properties.Resources.circle_orange_light;
        }

        private void pictureBoxverynegative_MouseEnter(object sender, EventArgs e)
        {
            pictureBoxverynegative.Image = Properties.Resources.circle_red;
        }

        private void pictureBoxverynegative_MouseLeave(object sender, EventArgs e)
        {
            setCurrentSentimentPicturebox();
            //pictureBoxverynegative.Image = Properties.Resources.circle_red_light;
        }

        private void pictureBoxverypositive_MouseHover(object sender, EventArgs e)
        {
            SetToolTip(pictureBoxverypositive);
        }

        private void pictureBoxpositive_MouseHover(object sender, EventArgs e)
        {
         //   SetToolTip(pictureBoxpositive);
        }

        private void pictureBoxneutral_MouseHover(object sender, EventArgs e)
        {
            SetToolTip(pictureBoxneutral);

        }

        private void pictureBoxnegative_MouseHover(object sender, EventArgs e)
        {
          //  SetToolTip(pictureBoxnegative);

        }

        private void pictureBoxverynegative_MouseHover(object sender, EventArgs e)
        {
            SetToolTip(pictureBoxverynegative);
        }

        private void SetToolTip(System.Windows.Forms.Control ctl)
        {
            System.Windows.Forms.ToolTip tt = new System.Windows.Forms.ToolTip();
            tt.SetToolTip(ctl, (string)ctl.Tag);
        }

       
        


  //    public void ShowMarsFormRegion()
  //      {
          //this.Manifest.ShowInspectorCompose = true;
          //  this.Manifest.ShowInspectorRead = true;
          //  this.Manifest.ShowReadingPane = true;
  //          this.Show();
   //     }

//      public void HideMarsFormRegion()
//      {
          //this.Manifest.ShowInspectorCompose = false;
          //this.Manifest.ShowInspectorRead = false;
          //this.Manifest.ShowReadingPane = false;
    //      this.Hide();
  //    }

      private void ShowHidebtn_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
      {
          ShowHideMarsAnalyticsPanel(!this.panel1.Visible);
      }

      public void ShowHideMarsAnalyticsPanel(bool bShow)
      {
          if (bShow)
          {
              ShowHidebtn.Text = "Hide";
              ShowHidebtn.Image = Properties.Resources.round_delete_small;
              panel1.Visible = true;
              this.btnpanel.Dock = System.Windows.Forms.DockStyle.Bottom;
           }
          else
          {
              ShowHidebtn.Text = "Show";
              ShowHidebtn.Image = Properties.Resources.round_checkmark_small;
              panel1.Visible = false;
              this.btnpanel.Dock = System.Windows.Forms.DockStyle.Top;
          }
      }
        
      private void button1_Click(object sender, EventArgs e)
      {
       //   ser = new SentimentResult();
       //   ser.Sentiment = 5;
       //   ser.Rating = 5;
       //  
          if (localMailItem != null)
          {
              
              try
              {
                  lblStatusMessage.Text = "Running...Please wait";
                  lblStatusMessage.Show();
                  this.UseWaitCursor = true;
                  Application.UseWaitCursor = true;
                  Cursor.Current = Cursors.WaitCursor;

                  richTextBox1.Text += "\n****** RUNNING SENTIMENT ANALYSIS ******\n";
                  ser = Globals.ThisAddIn.MARSEngine.SentimentAnalysis(localMailItem.Body,
                                                                       Globals.ThisAddIn.bCheckTrailLines,
                                                                       Globals.ThisAddIn.nLinesToSkip,
                                                                       Globals.ThisAddIn.bCheckHyperLinks,
                                                                       Globals.ThisAddIn.bCheckDateTime);

                  //this.SentimentValue.Text = findEmailSentiment("You are brilliant and good").ToString();
                  //this.SentimentValue.Text = findEmailSentiment("You are an idiot").ToString();
                  //this.SentimentValue.Text = findEmailSentiment("I am going to london").ToString();
                  //this.SentimentValue.Text = findEmailSentiment("Hello I need your help").ToString();

                  this.SentimentValue.Text = ser.Sentiment.ToString();
                  setCurrentSentimentPicturebox();
                  richTextBox1.Text += ser.SentimentResultsLogString;
                  lblStatusMessage.Text = "Run status....Completed";
              }
              catch(Exception ex)
              {
                  lblStatusMessage.Text = "Run status..Failed see log";
                  richTextBox1.Text += "\n****** SENTIMENT ANALYSIS Failed with exception ******\n"+"Error:"+ex.Message+"\nStack Trace:\n"+ex.StackTrace;
              }
              finally
              {
                  this.UseWaitCursor = false;
                  Application.UseWaitCursor = false;
                  Cursor.Current = Cursors.Default;

              }
              

          }
      }

        private void setCurrentSentimentPicturebox()
      {
          resetAllPictureboxes();

            if(ser != null)
            {
                ratingvalue.Text = ser.Rating.ToString();
                switch(ser.Sentiment)
                {

                    case -1:
                    case 1:
                        pictureBoxverynegative.Image = Properties.Resources.circle_red;
                        pictureBoxverynegative.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                        SentimentValue.Text = (string)pictureBoxverynegative.Tag;
                        break;
                        //pictureBoxnegative.Image = Properties.Resources.circle_orange;
                        //pictureBoxnegative.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                        //SentimentValue.Text = (string)pictureBoxnegative.Tag;
                        //break;

                    case 0:
                    case 2:
                        pictureBoxneutral.Image = Properties.Resources.round_grey;
                        pictureBoxneutral.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                        SentimentValue.Text = (string)pictureBoxneutral.Tag;
                        break;
                        
                    //case 3:
                    //    pictureBoxpositive.Image = Properties.Resources.circle_blue;
                    //    pictureBoxpositive.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    //    SentimentValue.Text = (string)pictureBoxpositive.Tag;
                    //    break;
                    case 3:
                    case 4:
                    case 5:
                        pictureBoxverypositive.Image = Properties.Resources.circle_green;
                        pictureBoxverypositive.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                        SentimentValue.Text = (string)pictureBoxverypositive.Tag;
                        
                        break;

                }
                
            }

      }
        private void resetAllPictureboxes()
        {
            pictureBoxverypositive.Image = Properties.Resources.circle_green_light;
            pictureBoxverypositive.BorderStyle = System.Windows.Forms.BorderStyle.None;
//            pictureBoxpositive.Image     = Properties.Resources.circle_blue_light;
//            pictureBoxpositive.BorderStyle = System.Windows.Forms.BorderStyle.None;
            pictureBoxneutral.Image      = Properties.Resources.round_grey_light;
            pictureBoxneutral.BorderStyle = System.Windows.Forms.BorderStyle.None;
//            pictureBoxnegative.Image     = Properties.Resources.circle_orange_light;
//            pictureBoxnegative.BorderStyle = System.Windows.Forms.BorderStyle.None;
            pictureBoxverynegative.Image = Properties.Resources.circle_red_light;
            pictureBoxverynegative.BorderStyle = System.Windows.Forms.BorderStyle.None;
        }

      private void label2_Click_1(object sender, EventArgs e)
      {

      }

      private void label12_Click(object sender, EventArgs e)
      {

      }

      private void panel1_Paint(object sender, PaintEventArgs e)
      {

      }

      private void pictureBoxverynegative_Click(object sender, EventArgs e)
      {

      }
    }
}
